package xhwl.hmo.mall.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

import java.util.ArrayList;
import java.util.List;

import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.adapter.PersonListAdapter;
import xhwl.hmo.mall.http.api.PersonListApi;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.http.bean.resp.PersonInfo;
import xhwl.hmo.mall.utils.SpUtil;
import xhwl.pulltorefresh.DefaultHeader;
import xhwl.pulltorefresh.PullToRefreshRecyclerView;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * Created by admin on 2017-11-06.
 */

public class PersonListActivity extends BaseActivity {
    private PullToRefreshRecyclerView recyclerView;
    int mPage = 1;
    PersonListAdapter adapter;
    List<PersonInfo> mList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_list);
    }

    @Override
    public void initViews() {
        initTitleViews();
        recyclerView = (PullToRefreshRecyclerView) findViewById(R.id.recyclerView);
        initRecylerView();

        adapter = new PersonListAdapter(mContext, mList);

    }
    private void initRecylerView() {

        //设置EmptyView
        View emptyView = View.inflate(mContext, R.layout.layout_empty_view, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        recyclerView.setEmptyView(emptyView);

        final LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        //设置是否开启上拉加载
        recyclerView.setLoadingMoreEnabled(false);
        //设置是否开启下拉刷新
        recyclerView.setPullRefreshEnabled(false);
        //设置是否显示上次刷新的时间
        recyclerView.displayLastRefreshTime(false);
//        recyclerView.setRefreshLimitHeight(DimenUtil.dip2px(mActivity,50));
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        recyclerView.getRefreshLayout().setRefreshHeader(new DefaultHeader(this){
            @Override
            public SpinnerStyle getSpinnerStyle() {
                return SpinnerStyle.Translate;
            }
        });
    }
    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        titleTextView.setText("保障人信息");
    }
    @Override
    public void initListener() {

    }

    @Override
    public void loadData() {
        getPersonList();

    }
    private void getPersonList(){
        String memberMobile= SpUtil.getString(SpUtil.MEMBER_MOBILE,"");
        HttpManager.getInstance().dealHttp(this, new PersonListApi(memberMobile), new OnNextListener<HttpResp<ArrayList<PersonInfo>>>() {

            @Override
            public void onNext(HttpResp<ArrayList<PersonInfo>> resp) {
                if(resp!=null&&resp.isOk()&&resp.data!=null&&resp.data.size()>0){
                    mList.clear();
                    mList.addAll(resp.data);
                }
                recyclerView.setAdapter(adapter);
            }
        });
    }
}
